/*
 * MySendAskListItem.java
 * business
 *
 * Created by SunDing on 2016/9/29.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.event.DeletePostedWantBuyEvent;
import com.yidiandao.business.data.response.AskToBuyInfoResponse;
import com.yidiandao.business.ui.adapter.AddPictureGridAdapter;
import com.yidiandao.business.ui.userabout.wanttobuy.detail.UserPostedWantToBuyDetailActivity;
import com.yidiandao.business.ui.userabout.wanttobuy.post.PostWantToBuyActivity;
import com.yidiandao.business.ui.widget.dialog.BusinessDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.MySendAskHolder;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.TimeUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 我发布的求购
 */
public class MySendAskListItem
        extends DisplayListItem<MySendAskHolder, AskToBuyInfoResponse.DataBean.WantBuyListBean> {


    private AddPictureGridAdapter mAdapter;

    public MySendAskListItem(AskToBuyInfoResponse.DataBean.WantBuyListBean bean) {
        super(bean);
    }

    @Override
    public void onShow(final Context context, final MySendAskHolder holder) {
        //date
        holder.mTvDateMonth.setVisibility(data.isShowTime() ? View.VISIBLE : View.GONE);
        holder.mViewSpace.setVisibility(!data.isShowTime() ? View.VISIBLE : View.GONE);
        holder.mTvDateMonth.setText(TimeUtils.formatMonth(TimeUtils.getAndroidMillis(data.getAddTime()), true));
        //advert
        ImageLoader.loadAvatar(context, holder.mIvAdv, data.getSex());
        //name
        holder.mTvName.setText(data.getDpName());
        //desc
        holder.mTvTitle.setText(data.getDesc());
        //pic
        List<String> mList = data.getPictures();
        if (null != mList && mList.size() > 0) {
            holder.mResizeGridView.setVisibility(View.VISIBLE);
            mAdapter = new AddPictureGridAdapter(context, mList, false);
            holder.mResizeGridView.setAdapter(mAdapter);
        } else {
            holder.mResizeGridView.setVisibility(View.GONE);
        }
        //数量
        holder.mTvNumber.setText(UIUtils.getString(R.string.ask_to_buy_number, data.getNumber(), data.getUnit()));
        //时间
        holder.mTvTime.setText(TimeUtils.getRelativeTimeWithSingleUnit(context.getResources(), TimeUtils.getAndroidMillis(data.getAddTime()), true));
        //报价
        holder.mTvOfferNum.setText(UIUtils.getString(R.string.ask_to_buy_popNUm, data.getOfferCount()));
        //新增报价
        holder.mTvNewNum.setVisibility(data.getPlusOfferCount() == 0 ? View.GONE : View.VISIBLE);
        holder.mTvNewNum.setText("+" + data.getPlusOfferCount());
        //求购id
        final String wantId = data.getWantId();
        switch (data.getApplySucc()) {
            case "0":
                //审核中
                holder.mIvStatus.setVisibility(View.VISIBLE);
                holder.mIvStatus.setImageResource(R.mipmap.ic_checking);
                holder.mTvEdit.setVisibility(View.GONE);
                holder.mTvDelete.setVisibility(View.GONE);
                break;
            case "1":
                //通过
                holder.mIvStatus.setVisibility(View.GONE);
                holder.mTvEdit.setVisibility(View.GONE);
                holder.mTvDelete.setVisibility(View.VISIBLE);
                break;
            case "2":
                //失败
                holder.mIvStatus.setVisibility(View.VISIBLE);
                holder.mIvStatus.setImageResource(R.mipmap.ic_check_refuse);
                holder.mTvEdit.setVisibility(View.VISIBLE);
                holder.mTvDelete.setVisibility(View.VISIBLE);
                break;
        }
        holder.mTvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteItemPost(context, wantId);
            }
        });
        holder.mTvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PostWantToBuyActivity.actionStart((Activity) context, wantId);
            }
        });
        holder.mResizeGridView.setTouchEventEnable();
        holder.mRootItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserPostedWantToBuyDetailActivity.actionStart((Activity) context, wantId);
            }
        });
    }

    private void deleteItemPost(Context context, final String wantId) {
        BusinessDialog.Builder builder = new BusinessDialog.Builder(context);
        builder.setTitle(R.string.dialog_tip7);
        builder.setPositiveButton(UIUtils.getString(R.string.dialog_sure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EventBus.getDefault().post(new DeletePostedWantBuyEvent(wantId));
                dialog.dismiss();
            }
        }).setNegativeButton(UIUtils.getString(R.string.dialog_cancel), null);
        BusinessDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public String getDisplayItemTag() {
        return MySendAskListItem.class.getSimpleName();
    }

    @Override
    public Class<MySendAskHolder> getViewHolderClazz() {
        return MySendAskHolder.class;
    }
}
